<?php

/**
 * @Description    通用模型, 所有模型继承此类
 * @Author         zhaojiping
 * @Copyright      Copyright (c) 山东艾思网络科技有限公司(https://www.aisisofit.cn)
 * @Date           2022/2/7
 * @CreateBy       VS Code
 */

namespace app\common\model;

use think\Model;

class CommonModel extends Model {

    protected $fields = true; // 通用查询字段
    protected $order = 'id desc'; // 通用排序字段
    public $with = []; // 关联模型

    public static function onBeforeWrite($row) {
    }

    public static function onAfterWrite($row) {
    }

    public static function onBeforeInsert($row) {
        if (isset($row->id)) unset($row->id); // 插入数据不允许上传主键
    }

    public static function onAfterInsert($row) {
    }

    public static function onBeforeUpdate($row) {
        if (isset($row->id)) unset($row->id);  // 更新数据不允许上传主键
    }

    public static function onAfterUpdate($row) {
    }

    public static function onBeforeDelete($row) {
    }

    public static function onAfterDelete($row) {
    }

    public static function onAfterRead($row) {
        // 返回数据前格式化约定好的字段
        if (isset($row['usable'])) // 禁用状态
            $row['usable_label'] = usable_label($row['usable']);

        if (isset($row['sex'])) // 性别
            $row['sex_label'] = sex_label($row['sex']);

        $arr = $row->toArray();
        foreach ($arr as $k => $v) {
            if (substr($k, 0, 3) == 'is_') // 是否
                $row[$k . '_label'] = is_label($v);
        }

        if (isset($row['create_time'])) // 日期格式化
            $row['create_date'] = date_format(date_create($row['create_time']), 'Y-m-d');
    }

    /**
     * 获取单条数据信息
     */
    public function geDetail($where = [], $field = true) {
        $field = $field !== true ? $field : $this->fields; // 先以直接调用为准, 再以子类重写的变量为准
        $list = self::field($field);
        if (!empty($this->with))
            $list->with($this->with);
        $data = $list->where($where)->find();
        return $data;
    }

    /**
     * 获取列表数据, 带分页
     */
    public function getPageList($where = [], $field = true, $order = 'id desc') {
        $p = input('p/d', 1);
        $page_size = input('page_size/d', 0);

        $field = $field !== true ? $field : $this->fields; // 先以直接调用为准, 再以子类重写的变量为准
        $order = $order !== 'id desc' ? $order : $this->order; // 先以直接调用为准, 再以子类重写的变量为准
        $data['page_size'] = $page_size !== 0 ? $page_size : (int)cache('config')['page_size']; // 以前端提交的为准, 再以后台定义的为准

        $list = self::where($where);
        if (!empty($this->with))
            $list->with($this->with);
        $data['count'] = $list->count();
        // echo $list->getlastsql();
        $data['list']  = $list->page($p, $data['page_size'])->field($field)->order($order)->select()->toArray();
        return $data;
    }

    /**
     * 获取列表数据, 不带分页
     */
    public function getList($where = [], $field = true, $order = 'id desc') {
        $field = $field !== true ? $field : $this->fields; // 先以直接调用为准, 再以子类重写的变量为准
        $order = $order !== 'id desc' ? $order : $this->order; // 先以直接调用为准, 再以子类重写的变量为准

        $list = self::where($where);

        if (!empty($this->with))
            $list->with($this->with);
        return $list->field($field)->order($order)->select()->toArray();
    }

    /**
     * 返回列表方式, 含有等级和排序分组, 如: 供下拉列表方式使用
     */
    public function getListByLevel($data, $pid = 0, $level = 0, $is_clear = true) {
        static $result = array();
        if ($is_clear) $result = array();
        if (!empty($data)) {
            foreach ($data as $key => $value) {
                if ($value['pid'] == $pid) {
                    $value['level'] = $level;
                    $result[] = $value;
                    $this->getListByLevel($data, $value['id'], $level + 1, false);
                }
            }
        }

        return $result;
    }

    /**
     * 生成树型结构
     */
    public function getTree($data, $pid = 0) {
        $child = [];
        foreach ($data as $value) {
            if (!isset($value['id']))
                continue;
            if ($value['pid'] == $pid)
                $child[$value['id']] = $value;
        }

        $tree = array();
        if (!empty($child)) {
            foreach ($child as $k => $v) {
                $child = $this->getTree($data, $v['id']);
                if (!empty($child)) $v['_child'] = $this->getTree($data, $v['id']);
                $tree[] = $v;
            }
        }
        return $tree;
    }
}
